package src.main.java.com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.waf.v1.WafClient;
import com.huaweicloud.sdk.waf.v1.model.*;
import com.huaweicloud.sdk.waf.v1.region.WafRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


public class ValuelistDemo {
    private static final Logger logger = LoggerFactory.getLogger(ValuelistDemo.class);

    public static void main(String[] args) {
        ICredential auth = new BasicCredentials()
                .withAk("<YOUR AK>")
                .withSk("<YOUR SK>");
        WafClient client = WafClient.newBuilder()
                .withCredential(auth)
                .withRegion(WafRegion.valueOf("cn-south-1"))
                .build();
        try {
            UpdateValueListResponse updateValueListResponse = updateValuelist(client);
        } catch (ConnectionException e) {
            logger.error("A connect timeout exception occurs while the WAF performs some certificate-related operations, exception: {}", e);
        } catch (RequestTimeoutException e) {
            logger.error("A request timeout exception occurs while the WAF performs some certificate-related operations, exception: {}", e);
        } catch (ServiceResponseException e) {
            logger.error("there is service response error, exception: {}", e);
            logger.error("HttpStatusCode: {}", e.getHttpStatusCode());
            logger.error("RequestId: {}", e.getRequestId());
            logger.error("ErrorCode: {}", e.getErrorCode());
            logger.error("ErrorMsg: {}", e.getErrorMsg());
        }

    }

    private static UpdateValueListResponse updateValuelist(WafClient client) {
        // 初始化引用表的值
        List<String> values = new ArrayList<>();
        values.add("/url2");
        // 初始引用表信息
        UpdateValueListRequest updateValueListRequest = new UpdateValueListRequest()
                .withValuelistid("Valuelistid")
                .withBody(new UpdateValueListRequestBody()
                        .withName("demo")
                        .withType(UpdateValueListRequestBody.TypeEnum.URL)
                        .withValues(values));
        // 更新引用表
        UpdateValueListResponse updateValueListResponse = client.updateValueList(updateValueListRequest);
        logger.info(updateValueListResponse.toString());
        return updateValueListResponse;
    }
}
